<?
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/classes/class.template_engine.php');
require_once('includes/lang/lang_'.$config['lang'].'.php');

// Connect to database
db_connect($config);

// Start Session
session_start();

// Check if this is an availability check from signup page using ajax
if(isset($_GET['avail']))
{
	// Check if anyone has this username
	$availcheck = mysql_num_rows(mysql_query("SELECT 1 FROM `".$config['db']['pre']."users` WHERE username='".addslashes($_GET['avail'])."' LIMIT 1"));

	if($availcheck)
	{
		// Someone already has this username
		echo $_GET['avail'].'|0';
	}
	else
	{
		// That username is available
		echo $_GET['avail'].'|1';
	}
	
	exit;
}

// Check if they have submitted the signup page
if(isset($_POST['username']))
{
	// Initiate error messages
	$errors = 0;
	$username_error = '';
	$password_error = '';
	$email_error = '';
	$agree_error = '';
		
	$_POST['username'] = strip_tags($_POST['username']);
	
	if(!eregi('[[:alnum:]]',$_POST['username']))
	{
		$errors++;
		$username_error = 'Username may only contain alpanumeric characters';
	}
	elseif( (strlen($_POST['username']) < 4) OR (strlen($_POST['username']) > 16) )
	{
		$errors++;
		$username_error = 'Username must be between 4 and 15 characters long';
	}
	else
	{
		$avail = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."users WHERE username='".addslashes($_POST['username'])."' LIMIT 1"));
		
		if($avail)
		{
			$errors++;
			$username_error = 'This username is unavailable';
		}
	}
	
	if( (strlen($_POST['password']) < 4) OR (strlen($_POST['password']) > 16) )
	{
		$errors++;
		$password_error = 'Password must be between 4 and 15 characters long';
	}
	elseif($_POST['password'] != $_POST['password2'])
	{
		$errors++;
		$password_error = 'The passwords you entered did not match';
	}
	
	if(trim($_POST['email']) == '')
	{
		$errors++;
		$email_error = 'Please enter an email address';
	}
	elseif(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['email'])) 
	{
		$errors++;
		$email_error = 'This is not a valid email address';
	}
	
	// Check they have agreed to the terms
	if(!isset($_POST['agree']))
	{
		$errors++;
		$agree_error = 'You need to accept the Terms and Conditions';
	}
	
	if($errors == 0)
	{
		$rem = md5(mt_rand(0,56)*time());
	
		mysql_query("INSERT INTO `".$config['db']['pre']."users` ( `user_id` , `username` , `password` , `email` , `remember` ) VALUES ('', '".addslashes($_POST['username'])."', '".addslashes(md5($_POST['password']))."', '".addslashes($_POST['email'])."', '".addslashes($rem)."');");
	
		$user_id = mysql_insert_id();
	
		$_SESSION['duser']['id'] = $user_id;
		$_SESSION['duser']['name'] = $_POST['username'];
	
		header("Location: index.php");
		exit;
	}
}

// Get site categories
$cats = get_cats($config);

// Load signup template
$page = new HtmlTemplate ("templates/" . $config['tpl_name'] . "/signup.html");
$page->SetParameter ('OVERALL_HEADER', create_header($config,$lang,$cats,'Signup'));
$page->SetParameter ('OVERALL_FOOTER', create_footer($config,$lang));
$page->SetLoop ('CATS', $cats);
if(isset($_POST['username']))
{
	$page->SetParameter ('USERNAME_FIELD', $_POST['username']);
	$page->SetParameter ('EMAIL_FIELD', $_POST['email']);
	
	$page->SetParameter ('USERNAME_ERROR', $username_error);
	$page->SetParameter ('PASSWORD_ERROR', $password_error);
	$page->SetParameter ('EMAIL_ERROR', $email_error);
	$page->SetParameter ('AGREE_ERROR', $agree_error);

}
else
{
	$page->SetParameter ('USERNAME_FIELD', '');
	$page->SetParameter ('EMAIL_FIELD', '');

	$page->SetParameter ('USERNAME_ERROR', '');
	$page->SetParameter ('PASSWORD_ERROR', '');
	$page->SetParameter ('EMAIL_ERROR', '');
	$page->SetParameter ('AGREE_ERROR', '');
}
if(isset($_SESSION['duser']['id']))
{
	$page->SetParameter ('LOGGEDIN', 1);
}
else
{
	$page->SetParameter ('LOGGEDIN', 0);
}
$page->SetParameter ('SITE_TITLE',$config['site_title']);
$page->CreatePageEcho($lang,$config);
?>